package com.demo.datastructure.binarySearch;

/**
 * @description: 二分法 找到重复元素中的最后一个元素
 * @author: ljr
 * @time: 2021/5/14 11:08
 */
public class BinarySearch6 {

    public static int bsearch(int[] a, int n, int value) {
        int low =  0;
        int high = n-1;
        while(low <= high){
            int mid = low+(high-low >> 1);
            if(a[mid] > value){
                high = mid -1;
            }else if(a[mid] <= value){
                low = mid+1;
            }
        }
        if(high != 0 && a[high] == value){ return  high;}

        return  -1;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,3,4,5,6,8,8,8,8,18};
        int num = BinarySearch6.bsearch(nums,nums.length,8);
        System.out.print(num);
    }

}
